#ifndef MD_USER_HANDLE_H
#define MD_USER_HANDLE_H

#include "./ctp/ThostFtdcMdApi.h"
#include <iostream>
#include <unistd.h>
#include <nlohmann/json.hpp>
#include "tcp_server.h"
#include "logger.h"

using json = nlohmann::json;

// 声明外部变量
extern int nRequestID;

class CMduserHandler : public CThostFtdcMdSpi
{
private:
    CThostFtdcMdApi *m_mdApi;
    std::string m_brokerID;
    std::string m_userID;
    std::string m_password;
    std::string m_flow_path;
    std::string m_front_address;
    TcpServer* m_tcp_server;

public:
    CMduserHandler(const std::string& brokerID = "", 
                  const std::string& userID = "", 
                  const std::string& password = "",
                  const std::string& flow_path = "",
                  const std::string& front_address = "");
    ~CMduserHandler();
    
    void connect();
    void login();
    void subscribe(const std::string& instrument);
    void unsubscribe(const std::string& instrument);
    void release();
    
    // 继承自CThostFtdcMdSpi的虚函数实现
    virtual void OnRtnDepthMarketData(CThostFtdcDepthMarketDataField *pDepthMarketData);

    void setServer(TcpServer* server);
    // virtual void OnRspAuthenticate(CThostFtdcRspAuthenticateField *pRspAuthenticateField, CThostFtdcRspInfoField *pRspInfo, int nRequestID, bool bIsLast);
    void reqAuthenticate();
    void OnFrontConnected();
    void OnFrontDisconnected(int nReason);
};

#endif // MD_USER_HANDLE_H
